﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace DreamBox.Net.Core
{
    public class Picture
    {
        /// <summary>
        /// 根据文件流生成缩略图
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Image Thumbnail(Stream stream,int width,int height)
        {
            Image img= Image.FromStream(stream);
            Image.GetThumbnailImageAbort call=null;
            return img.GetThumbnailImage(width, height,call , new System.IntPtr());
        }
        /// <summary>
        /// 缩略图处理方法
        /// </summary>
        /// <param name="path"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public string Thumbnail(string path, int width, int height)
        {
            //获取文件扩展名
            string kz_name = Path.GetExtension(path).ToLower();
            //设置新的文件名 在原来的文件名
            string filename = Path.GetFileName(path);
            //开始生成缩略图
            Image.GetThumbnailImageAbort call = null;
            Image flagimg = Image.FromFile(path);
            Image small = flagimg.GetThumbnailImage(width, height, call, new System.IntPtr());

            string ph = path.Replace(filename, "");
            //获取新的存放路径
            string file = filename.Replace(kz_name, "");
            string thum_path = ph + "thumbnail_" + file + kz_name;
            //保存缩略图
            small.Save(thum_path);
            //释放资源
            flagimg.Dispose();
            small.Dispose();
            return thum_path;
        }
    }
}
